using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Npc : MonoBehaviour
{
    // Start is called before the first frame update
    private enum State
    {
        Idle,
        Attack,
        Walk
    };
    private State curState;
    public float runSpeed = 5f;
    public Transform target;
    public float FollowDistance;
    public float AttackDistance;
    public float attackRadius = 1f;
    public Transform attackPoint;

    private bool damageMade = false;
    private float Health = 100f;
    public Slider healthSlider;
    public GameObject gameManagerment;

    private CharacterController npc_controller;
    private Animator npc_animator;
    private void Awake()
    {
        curState = State.Idle;
        npc_animator = GetComponent<Animator>();
        npc_controller = GetComponent<CharacterController>();
        healthSlider.value = Health * 0.01f;
    }

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        npc_controller.checkGround();
        float distance = 0f;
        if (target.gameObject.activeSelf == true)
        {
            distance = Vector3.Distance(target.transform.position, transform.position);
        }
        else
        {
            distance = 100f;
        }
        if (distance < AttackDistance)
        {
            curState = State.Attack;
            npc_animator.SetTrigger("Attack");
        }
        else if (distance < FollowDistance)
        {
            curState = State.Walk;
            npc_animator.SetBool("IsWalk", true);
        }
        else
        {
            curState = State.Idle;
            npc_animator.SetBool("IsWalk", false);
        }
        if (attackPoint.gameObject.activeSelf == true && damageMade == false)
        {
            damageMade = true;
            Collider2D[] hittedObjects = Physics2D.OverlapCircleAll(attackPoint.position, attackRadius);
            if (hittedObjects.Length > 0)
            {
                for (int i = 0; i < hittedObjects.Length; i++)
                {
                    if (hittedObjects[i].gameObject != gameObject)
                    {
                        Player player = hittedObjects[i].gameObject.GetComponent<Player>();
                        if (player != null)
                        {
                            player.Hurt(20);
                        }
                    }
                }
            }
        }
        else if (attackPoint.gameObject.activeSelf == false && damageMade == true)
        {
            damageMade = false;
        }
        walk();
    }

    private void walk()
    {
        if (!npc_controller.isGrounded())
        {
            npc_animator.SetBool("IsWalk", false);
            return;
        }
        float move_direction = target.position.x - transform.position.x;
        if (move_direction < 0)
        {
            if (transform.localScale.x > 0f)
                transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
        }
        if (move_direction > 0)
        {
            if (transform.localScale.x < 0f)
                transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
        }
        if (curState == State.Walk)
        {
            if (move_direction < 0)
            {
                npc_controller.Move(-1f * new Vector3(runSpeed, 0f, 0f));
            }
            else if (move_direction > 0)
            {
                npc_controller.Move(1f * new Vector3(runSpeed, 0f, 0f));

            }
        }
    }
    public void Hurt(int damage)
    {
        npc_animator.SetTrigger("Hurt");
        curState = State.Idle;
        Health -= damage;
        GetComponent<AudioSource>().clip = Resources.Load("bgm/warrior_hurt") as AudioClip;
        GetComponent<AudioSource>().Play();
        if (Health <= 0)
        {
            gameManagerment.GetComponent<AudioSource>().clip = Resources.Load("bgm/player_win") as AudioClip;
            gameManagerment.GetComponent<AudioSource>().Play();
            Health = 0;
            npc_animator.SetTrigger("Die");
        }
        healthSlider.value = Health * 0.01f;
    }
    public void Dead()
    {
        healthSlider.gameObject.SetActive(false);
        this.gameObject.SetActive(false);
        target.gameObject.GetComponent<Animator>().SetTrigger("win");

    }


}
